package com.sun;

import java.util.ArrayList;
import java.util.List;

/**
 * https://leetcode.cn/problems/remove-duplicates-from-sorted-array/
 */
public class Test26删除有序数组中的重复项 {
    public static void main(String[] args) {
        int[] nums = {0,0,1,1,1,2,2,3,3,4};
        int res = removeDuplicates2(nums);
        System.out.println("res = " + res);
        for (int num : nums) {
            System.out.println(num);
        }
    }

    /**
     * 方法一：自己
     */
    public static int removeDuplicates1(int[] nums) {
        int index = 1;
        int temp = nums[0];
        int n = nums.length;
        for (int i = 1; i < n;) {
            while (nums[i] == temp || nums[i] < temp){
                i++;
                if (i >= n) return index;
            }
            temp = nums[index];
            nums[index] = nums[i];
            nums[i]=temp;
            temp = nums[index];
            index++;
            i++;
        }
        return  index;
    }

    /**
     * 方法二：力扣官方双指针
     */
    public static int removeDuplicates2(int[] nums){
        if (nums == null || nums.length == 0) return 0;
        int p = 0;
        int q = 1;
        while (q < nums.length){
            if (nums[q] != nums[p]){
                nums[++p] = nums[q];
            }
            q++;
        }
        return ++p;
    }

}
